<?php
class sms_model_fictiondata extends Model {

		public function __construct(){
		parent::__construct();
				$this->fic = 'fiction_subscribe';
				$this->cancel_dkp = 'dkp_cancel_subscribe';
		}
		
		public function getfictionListUid($uid, $page, $pagesize)
		{	
			$sql	=  " select * from " . $this->fic . " where sub_uid = $uid  order by sub_uptime desc " ;
			$params =  array('uid'=>$uid);
			$result =  $this->db->selectPage($sql, $params, $page, $pagesize );	
			$sql =  " update " . $this->fic . " set sub_readed = 1 where sub_uid = " . $uid ;
			$this->db->query($sql);
			return $result;	
		}
		
		//查询某个用户 对某小说的订阅
		public function checkSubscribeState($uid, $sub_id)
		{
			$sql	= " select * from " . $this->fic . " where sub_uid = $uid and  `sub_id` = $sub_id  "; 
			$result = $this->db->fetchRow($sql);	
			return  $result ;		
		}
		
		//更新 小说信息
		public function updateFictionData($data)
		{ 
			$num = count($data);
			$arr_uid = array();
			for ($i=0; $i<$num ; $i++)
			{
				$sql_uid = ' select `sub_uid`   from ' . $this->fic . ' where sub_id = ' . $data[$i]['sub_id'] .' and  sub_readed = 1 ';
				$result_uid  = $this->db->fetchAll($sql_uid);
				if ($result_uid)  
				{
						$arr_uid = array_merge($arr_uid ,$result_uid);
				}				
				$sql_update = " update "  . $this->fic . " set sub_update = '" . $data[$i]['sub_update'] . "' , sub_url = '" . $data[$i]['sub_url'] . "' , sub_readed = 0 , `sub_uptime` = " .time(). "  where sub_id  =  " . $data[$i]['sub_id'] ;
				$this->db->query($sql_update);
			}
			
			foreach($arr_uid as $v) {
					$arr[] = $v['sub_uid'];
			}
			
			$num = count($arr);
			$k = 1; $m = 0;
			$array = array();
			for ($i=0;$i<$num;$i++){
				if($arr[$i]) {					
					for($j=$i+1;$j<$num;$j++){
						if($arr[$i] == $arr[$j]){
							 $k++;  unset($arr[$j]);
						}
					}
					if($k == 1)		$array['str_uid'][] = $arr[$i];
					else {
						$array['arr'][$m]['uid'] = $arr[$i];
						$array['arr'][$m]['num'] = $k;
						$m++;
					}
					$k = 1 ;
				}
			}
			return $array;
		}
		
		//判断用户是否订阅此小说
		public function hasSubscribe($uid , $sub_id)
		{
			if ($sub_id) {
				$result = $this->checkSubscribeState($uid, $sub_id);
				if ($result) return true;
				else return false ;
					
			}
		}
		
		//添加一条订阅
		public function addSubscribe($arguments)
		{
			
			$arguments = filter_params($arguments, 
						'sub_uid', 'sub_id', 'sub_id_url', 'sub_name', 'sub_img', 'sub_update', 'sub_url' );
			$arguments['sub_readed']	=	1 ;
			$arguments['sub_time']		=  time() ;
			$arguments['sub_uptime']	=  time() ;
			$key = 'subscribe_count_2'.$arguments['sub_id'];
			Cache::getInstance()->remove($key);
			return $this->db->insert($this->fic, $arguments);
			
		}
		
		//取消订阅
		public function cancelSubscribe($uid, $subid)
		{
			$sql = " select * from  " . $this->fic . " where subid = $subid ";
			$data = $this->db->fetchRow($sql);
			if ($data && $data['sub_uid'] == $uid)
			{			
				$sql = " delete from " . $this->fic . " where subid = $subid  ";
				$resutl = $this->db->query($sql);
				
				$cacheKey		=  'subscribe'.$uid;
				$subscribeData	= Cache::getInstance()->load($cacheKey);
				$num = array_search($data['sub_id'],$subscribeData['fiction']) ;
				if ($num   || $num === 0 ) {					
					unset($subscribeData['fiction'][$num]);
					Cache::getInstance()->save($subscribeData, $cacheKey, array(), 2592000);
				}
				
				$array = array('uid' => $uid,  'res_id' => $data['sub_id'] , 'type' => 3 , 'dateline' => time() );				
				$this->db->insert($this->cancel_dkp, $array);	
				
				$key = 'subscribe_count_2'.$data['sub_id'];
			    Cache::getInstance()->remove($key);		
				
				return $data['sub_id'] ;
		   }
		}
		
		//判断一个用户的订阅数
		public function isMaxSubscribe($uid)
		{
			$sql	= " select count(*) as num from " . $this->fic . " where `sub_uid` = $uid " ;			
			$result = $this->db->fetchRow($sql);
			if ($result['num'] <= 99) return true ;
			else return false ;
		}
		
		//key验证
		public function checkKeyUid($key_id, $key)
		{
			$str = '178fiction_sns';
			$checkkey = md5($key_id . $str);
			if ($key == $checkkey ) return true ;
			else return false ;
		}
		
		public function getSubscribeStateByUid($uid)
		{
			$sql	= " select sub_id from " . $this->fic . " where `sub_uid` = $uid " ;
			$fic_id = $this->db->fetchAll($sql);
			$sql	= " select sub_id from cartoon_subscribe where `sub_uid` = $uid " ;
			$cart_id = $this->db->fetchAll($sql);
			$array = array();
			if ($fic_id) {
				foreach ($fic_id as $v) {
					$array['fiction'][] = $v['sub_id'] ; 
				}
			}
			if ($cart_id) {
				foreach ($cart_id as $v) {
					$array['cartoon'][] = $v['sub_id'] ;
				}
			}
			return  $array;
		}
}